package com.letv.whatslive.model.convert;

import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.AppInfo;
import com.letv.whatslive.model.GlobalResource;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.commons.lang3.StringUtils;

//2016.1.6

/**
 * Created by shenyue on 2016/1/6.
 */
public class AppInfoConvert {
    public static DBObject castAppInfoToDBObject(AppInfo appInfo){
        DBObject dbo = new BasicDBObject();
        if(appInfo.getId()!=null){
            dbo.put("_id",appInfo.getId());
        }

        if(appInfo.getDeviceType()!=null){
            dbo.put("deviceType",appInfo.getDeviceType());
        }else{
            dbo.put("deviceType",0);
        }
        if(appInfo.getPriority()!=null){
            dbo.put("priority",appInfo.getPriority());
        }else{
            dbo.put("priority",0);
        }
        if(StringUtils.isNotBlank(appInfo.getName())){
            dbo.put("name",appInfo.getName());
        }else{
            dbo.put("name","");
        }
        if(StringUtils.isNotBlank(appInfo.getDescribe())){
            dbo.put("describe",appInfo.getDescribe());
        }else{
            dbo.put("describe","");
        }
        if(StringUtils.isNotBlank(appInfo.getDownLoadUrl())){
            dbo.put("downLoadUrl",appInfo.getDownLoadUrl());
        }else{
            dbo.put("downLoadUrl","");
        }
        if(StringUtils.isNotBlank(appInfo.getIconUrl())){
            dbo.put("iconUrl",appInfo.getIconUrl());
        }else{
            dbo.put("iconUrl","");
        }
        if(appInfo.getIsOpen()!=null){
            dbo.put("isOpen",appInfo.getIsOpen());
        }else{
            dbo.put("isOpen",0);
        }
        dbo.put("createTime", ObjectUtils.toLong(appInfo.getCreateTime(), System.currentTimeMillis()));


        return dbo;


//        DBObject dbo = new BasicDBObject();
//        if (device.getId() != null) {
//            dbo.put("_id", device.getId());
//        }
//        dbo.put("platformId",device.getPlatformId());
//        dbo.put("sysVer", device.getSysVer());
//        dbo.put("model", device.getModel());
//        dbo.put("appVer", device.getAppVer());
//        dbo.put("corporationId", device.getCorporationId());
//        dbo.put("channelId", device.getChannelId());
//        return dbo;
    }

    public static AppInfo castDBObjectToAppInfo(DBObject dbObject){
        AppInfo appInfo = new AppInfo();
        if(dbObject != null) {
            appInfo.setId(ObjUtils.toLong(dbObject.get("_id")));
            appInfo.setName(ObjUtils.toString(dbObject.get("name")));
            appInfo.setDeviceType(ObjUtils.toInteger(dbObject.get("deviceType")));
            appInfo.setPriority(ObjUtils.toLong(dbObject.get("priority")));
            appInfo.setDescribe(ObjUtils.toString(dbObject.get("describe")));
            appInfo.setDownLoadUrl(ObjUtils.toString(dbObject.get("downLoadUrl")));
            appInfo.setIconUrl(ObjUtils.toString(dbObject.get("iconUrl")));
            appInfo.setCreateTime(ObjUtils.toLong(dbObject.get("createTime")));
            appInfo.setIsOpen(ObjUtils.toInteger(dbObject.get("isOpen")));
        }

        return appInfo;
    }
}
